package hibernate.method;

import hibernate.method.interfaces.NewsMethodsInterface;
import hibernate.model.News;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;

public class NewsMethods implements NewsMethodsInterface{

	@SuppressWarnings("unchecked")
	@Override
	public List<News> listAllNews(SessionFactory sessionFactory, Session session) {
		Criteria cr = session.createCriteria(News.class);
		cr.addOrder(Order.desc("add_date"));
		return cr.list();
	}

	@Override
	public void addNews(SessionFactory sessionFactory, Session session,
			String subject, String content) {
		News n = new News();
		n.setSubject(subject);
		n.setContent(content);
		session.save(n);
	}

	@Override
	public void deleteNews(SessionFactory sessionFactory, Session session,
			News n) {
		session.delete(n);
		
	}

	@Override
	public News idToNews(SessionFactory sessionFactory, Session session,
			int news_id) {
		Object object = session.load(News.class, news_id);
		News msg = (News) object;
		return msg;
	}

	
	
	
}
